package com.example.bootdemo.controller;

import com.alibaba.fastjson.JSON;
import com.example.bootdemo.pojo.Role;
import com.example.bootdemo.pojo.ServiceResponse;
import com.example.bootdemo.pojo.User;
import com.example.bootdemo.pojo.UserRole;
import com.example.bootdemo.service.UserSecurityService;
import com.example.bootdemo.service.impl.UserDetailServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("security")
public class UserSecurityController {

   private static final Logger logger = LoggerFactory.getLogger(UserSecurityController.class);

   @Autowired
   private UserSecurityService userSecurityService;

   @Autowired
   private UserDetailServiceImpl userDetailService;

   @PostMapping("/user/write")
    public ServiceResponse<User> user(@RequestBody User user){
        logger.info("user={}",JSON.toJSON(user));
        ServiceResponse<User> response = new ServiceResponse<>();
        User returnUser = userSecurityService.saveUser(user);
        response.setData(returnUser);
        return response;
    }

    @DeleteMapping("/user/write")
    public ServiceResponse<String> user(@RequestParam("username") String username){

       logger.info("username={}",username);

        ServiceResponse<String> response = new ServiceResponse<>();

        userSecurityService.deleteByUsername(username);

        response.setData("delete user success");

        return response;

    }

    @GetMapping("/user/read")
    public ServiceResponse<User> queryUser(@RequestParam("username") String username){

       logger.info("username={}",username);
       ServiceResponse<User> response = new ServiceResponse<>();
       User user = (User) userDetailService.loadUserByUsername(username);
       response.setData(user);
       return response;

    }


    @PostMapping("/role/write")
    public ServiceResponse<Role> role(@RequestBody Role role){

       logger.info("role={}",JSON.toJSON(role));

       ServiceResponse<Role> response = new ServiceResponse<>();

       Role role1 = userSecurityService.saveRole(role);

       response.setData(role1);

       return response;
    }

    @DeleteMapping("/role/write")
    public ServiceResponse<String> role(@RequestParam("roleId") String roleId){

       logger.info("roleId={}",roleId);

       ServiceResponse<String> response = new ServiceResponse<>();

       userSecurityService.deleteRoleByRoleId(roleId);

       response.setData("delete role success");

       return response;
    }

    @PostMapping("/user/role/write")
    public ServiceResponse<UserRole> userRole(@RequestBody UserRole userRole){

       logger.info("userRole={}",JSON.toJSON(userRole));

       ServiceResponse<UserRole> response = new ServiceResponse<>();

       UserRole resUserRole = userSecurityService.saveUserRole(userRole);

       response.setData(resUserRole);

       return response;
    }

    @DeleteMapping("/user/role/write")
    public ServiceResponse<String> userRole(@RequestParam("userId") Long userId,@RequestParam("roleId") String roleId){

       logger.info("userId={},roleId={}",userId,roleId);

       ServiceResponse<String> response = new ServiceResponse<>();

       userSecurityService.deleteUserRoleByUnionId(userId,roleId);

       return response;
    }




}
